/**
 * Created by fupeng on 17/6/21.
 */
var tool = require('../../common/utils/appUtils');
var moment = require('moment');
var async = require('async');

module.exports = {
    /**
     *  保存通过扫描二维码进入的用户openID
     * @param params
     * @param callback
     */
    saveSpreader:function(params, callback){
        tool.baseDao('','select member_id from member_user where openid = ?',params.openid,function(result){
            if(!result.isSuccess) {
                callback('err');
            }

            if (result.rows.length == 0) {
                callback('err');
            }else {
                tool.baseDao('','select id from spreader where member_id = ?',result.rows[0]['member_id'],function(data){
                    if(data.rows.length == 0){
                        tool.baseDao('','insert into spreader (member_id,qrcodeaddress)values(?,?)',[result.rows[0]['member_id'],params.qrcodeaddress],function (result) {
                            if(!result.isSuccess) {
                                callback('err');
                            }
                            if (result.rows.affectedRows > 0) {
                                callback(null, {
                                    message: '插入成功'
                                })
                            }
                        })
                    }

                })
            }

        })
    },
    getSpreaderbyopenid:function(params,callback){
        async.waterfall([
            function (callback) {
                tool.baseDao('','select member_id from member_user where openid = ?',params.open_id,function(result) {
                    if (!(result.isSuccess)) {
                        callback('err');
                    }
                    if (result.rows.length == 0) {
                        callback('err');
                    }else {
                        callback(null, result.rows[0]['member_id'])
                    }
                })
            },
            function (arg,callback) {
                tool.baseDao('','SELECT	s.*, m.* ,(SELECT	count(*) FROM	spreaderdata sd WHERE sd.member_id = s.member_id	) AS total FROM spreader s LEFT JOIN member_user m ON m.member_id = s.member_id where s.member_id = ?',arg,function (reslut) {
                    if (!(reslut.isSuccess)) {
                        callback('err');
                    }else {
                        callback(null,reslut.rows[0]);
                    }
                })
            }
        ],function (err, result){
            if(err) {
                callback('err');
            }else if(result) {
               callback(null,result);
            }
        })


    },


    saveScanOpenid:function (params, callback) {
        tool.baseDao('','select * from spreaderdata where member_id = ? and open_id = ?',[params.member_id,params.open_id],function(result){
            if(!result.isSuccess) {
                callback('err');
            }

            if (result.rows.length > 0) {
                callback(null,{
                    message:'已经扫过此码'
                })
            }else {
                tool.baseDao('','insert into spreaderdata (member_id,open_id)values(?,?)',[params.member_id,params.open_id],function (result) {
                    if(!result.isSuccess) {
                        callback('err');
                    }
                    if (result.rows.affectedRows > 0) {
                        callback(null, {
                            message: '推广成功'
                        })
                    }
                })
            }

        })
    },

    getSpreader:function(params,callback){
        var sql = 'SELECT	s.*, m.* ,(SELECT	count(*) FROM	spreaderdata sd WHERE sd.member_id = s.member_id	) AS total FROM	spreader s LEFT JOIN member_user m ON m.member_id = s.member_id ';
        if(params.nickname){
            sql +='WHERE m.nickname LIKE  "%'+ params.nickname +'%"'
        }
         sql +=' ORDER BY total desc LIMIT ?,20';
        tool.baseDao('',sql,[parseInt(params.lastIndex)],function(result){
            if(!result.isSuccess) {
                callback('err');
            }else{
                callback(null,result.rows)
            }
        })
    }
}